include-html.js ➔ includeHtml   C
last analyzed

Complexity

Conditions 10

Size

Total Lines 57
Code Lines 25

Duplication

Lines 57
Ratio 100 %

Importance

Changes 0
Metric Value
eloc 25
dl 57
loc 57
rs 5.9999
c 0
b 0
f 0
cc 10

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Complexity

Complex classes like include-html.js ➔ includeHtml often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1 View Code Duplication
function includeHtml(url, target, error, success) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
2
    var xhttp;
3
4
    var el = new E(target);
0 ignored issues
show
Bug introduced by
The variable E seems to be never declared. If this is a global, consider adding a /** global: E */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
5
    var elmnt = el.first();
6
7
    if (typeof success !== 'function') {
8
        success = function () {
9
            console.log('includeHtml success', "included");
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
10
        }
11
    }
12
13
    if (typeof error !== 'function') {
14
        error = function () {
15
            console.log('includeHtml error', "Page not found.");
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
16
        }
17
    }
18
    console.log('includeHtml url', url);
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
19
20
    if (url) {
0 ignored issues
show
Complexity Best Practice introduced by
There is no return statement if url is false. Are you sure this is correct? If so, consider adding return; explicitly.

This check looks for functions where a return statement is found in some execution paths, but not in all.

Consider this little piece of code

function isBig(a) {
    if (a > 5000) {
        return "yes";
    }
}

console.log(isBig(5001)); //returns yes
console.log(isBig(42)); //returns undefined

The function isBig will only return a specific value when its parameter is bigger than 5000. In any other case, it will implicitly return undefined.

This behaviour may not be what you had intended. In any case, you can add a return undefined to the other execution path to make the return value explicit.

Loading history...
21
        /* Make an HTTP request using the attribute value as the url name: */
22
        xhttp = new XMLHttpRequest();
23
        xhttp.onreadystatechange = function () {
24
            console.log('includeHtml el_id', target);
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
25
26
            if (this.readyState == 4) {
27
                if (this.status == 200) {
28
                    // console.log('elmnt', elmnt);
29
                    // console.log('responseText', this.responseText);
30
                    // elmnt.innerHTML = this.responseText;
31
                    // elmnt.appendChild(this.responseText);
32
                    // elmnt.insertAdjacentHTML('beforeend', this.responseText);
33
                    // var e = document.createElement('div');
34
                    // e.innerHTML = this.responseText;
35
                    // while(e.firstChild) {
36
                    // elmnt.appendChild(e);
37
                    // }
38
39
                    // elmnt.insertAdjacentHTML('afterend', this.responseText);
40
                    elmnt.insertAdjacentHTML('beforeend', this.responseText);
41
42
                    success(this);
0 ignored issues
show
Bug introduced by
The call to success seems to have too many arguments starting with this.
Loading history...
43
                }
44
                if (this.status == 404) {
45
                    elmnt.innerHTML = "includeHtml Page not found.";
46
                    error(this);
0 ignored issues
show
Bug introduced by
The call to error seems to have too many arguments starting with this.
Loading history...
47
                }
48
                /* Remove the attribute, and call this function once more: */
49
                // includeHtml(url, success, error);
50
            }
51
        }
52
        xhttp.open("GET", url, true);
53
        xhttp.send();
54
        /* Exit the function: */
55
        return this;
56
    }
57
}
58